package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Zc;
import com.hopu.service.ZcService;
import com.hopu.util.ResultUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RequestMapping("/zc")
@RestController
public class ZcController {
    @Resource
    private ZcService zcServiceImpl;

    @PostMapping("/findAllZc")
    private ResultUtils findAllZc(@RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            List<Zc> allZc = zcServiceImpl.findAllZc();

            PageInfo pageInfo = new PageInfo(allZc);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }
    }

    @PostMapping("addZc")
    public ResultUtils addZc(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "ctitle")String ctitle,
            @RequestParam(value = "cremark")String cremark

    ){
        try {
            //Emp empLog = (Emp) request.getSession().getAttribute("empLog");

            int num = zcServiceImpl.addZc(ctitle,new Date().getTime(),cremark);

            if (num==0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/findZcByCid")
    public ResultUtils findZcByCid(int cid){

        Zc zc = zcServiceImpl.findZcByCid(cid);

        return new ResultUtils(200,"查询页面成功",zc);

    }
    @PostMapping("uppZc")
    public ResultUtils uppZc(Zc zc, HttpSession session){


            long time = new Date().getTime();
            zc.setCtime(time);
            int biao1 = zcServiceImpl.uppZc(zc);
            if (biao1<0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);
    }


}
